using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;


namespace DAL
{
    public class DataProvider
    {
        private OleDbDataAdapter adapter;
        private OleDbConnection connection;

        string ConnectionString
        {
            get
            {
                try
                {
                    return "provider=Microsoft.Jet.oledb.4.0;data source=qlsv2000.mdb";
                }
                catch
                {
                    return "";
                }
            }
            set
            {
            }
        }

        public DataProvider()
        {
            connection = new OleDbConnection(ConnectionString);
            adapter = new OleDbDataAdapter();
        }

        private OleDbConnection OpenConnection()
        {
            if (connection.State == ConnectionState.Closed || connection.State == ConnectionState.Broken)
            {
                try
                {
                    connection.Open();
                }
                catch
                {
                    MessageBox.Show("Loi noi ket", "Loi");
                }
            }
            return connection;
        }

        public DataTable ExecuteSelectQuery(String query, OleDbParameter[] parameters)
        {
            OleDbCommand command = new OleDbCommand();
            DataTable datatable = new DataTable();
            datatable = null;
            DataSet ds = new DataSet();
            try
            {
                command.Connection = OpenConnection();
                command.CommandText = query;
                if (parameters != null)
                    command.Parameters.AddRange(parameters);
                command.ExecuteNonQuery();
                adapter.SelectCommand = command;
                adapter.Fill(ds);
                datatable = ds.Tables[0];
            }
            catch //(OleDbException e)
            {
                MessageBox.Show("Loi thu hanh Select");//("Error - ExecuteSelectQuery: " + query + " \nException: " + e.StackTrace.ToString(), "Error");
                return null;
            }
            return datatable;
        }

        public bool ExecuteNonSelectQuery(String query, OleDbParameter[] parameter)
        {
            OleDbCommand command = new OleDbCommand();
            try
            {
                command.Connection = OpenConnection();
                command.CommandText = query;
                command.Parameters.AddRange(parameter);
                adapter.UpdateCommand = command;
                command.ExecuteNonQuery();
            }
            catch //(OleDbException e)
            {
                MessageBox.Show("Loi cap nhat");//("Error - ExecuteNonSelectQuery : " + query + " \nException: " + e.StackTrace.ToString(), "Error");
                return false;
            }
            return true;
        }

    }
}
